package com.qf.controller;

import com.qf.dao.UserDao;
import com.qf.pojo.User;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.stylesheets.LinkStyle;

import java.util.List;

/**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/6/30 21:55
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/insert")
    public String insertUser(@RequestBody User user){
        int count = userService.insert(user);
        if (count>0){
            return "添加用户成功!";
        } else {
            return "添加用户失败!";
        }
    }

    @RequestMapping("/update")
    public String update(@RequestBody User user){
        int count = userService.update(user);
        if (count>0){
            return "修改用户成功!";
        } else {
            return "修改用户失败!";
        }
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam Integer id){
        int count = userService.delete(id);
        if (count>0){
            return "删除用户成功!";
        } else {
            return "删除用户失败!";
        }
    }

    @RequestMapping("/select")
    public List<User> select( String name){
        List<User> list = userService.findUserByName(name);
        if (list != null){
            System.out.println(list);
            return list;
        } else {
            return null;
        }
    }

    /**
     * 查询所有用户
     * @param
     * @return
     */
    @RequestMapping("/selectAll")
    public List<User> selectAll(){
        List<User> list = userService.findAllUser();
        if (list != null){
            System.out.println(list);
            return list;
        } else {
            return null;
        }
    }
}
